package com.junmo.common.redis;

import cn.hutool.core.util.StrUtil;
import com.junmo.common.redis.enums.RedisConnectionType;
import com.junmo.common.redis.properties.RedisProperty;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 分布式锁 Redisson 配置
 *
 * @author huawei
 * @email huawei_code@163.com
 * @date 2021/2/22
 */
@Slf4j
@EnableConfigurationProperties(RedisProperty.class)
@Configuration
public class RedissonConfig {

  @Autowired
  private RedisProperty redisProperty;

  @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress(redisProperty.getAddress());
        singleServerConfig.setDatabase(redisProperty.getDatabase());
        //singleServerConfig.setConnectionMinimumIdleSize(redisProperty.getMinIdle());
        if (StrUtil.isNotBlank(redisProperty.getPassword())) {
            singleServerConfig.setPassword(redisProperty.getPassword());
        }
        return Redisson.create(config);
    }

}
